package com.dyzx.hbb.modules.wms.controller;


import cn.hutool.core.bean.BeanUtil;
import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.common.config.MyLambdaQueryWrapper;
import com.dyzx.hbb.common.controller.BaseValidController;
import com.dyzx.hbb.common.utils.Utils;
import com.dyzx.hbb.modules.wms.entity.WmsHouse;
import com.dyzx.hbb.modules.wms.entity.WmsLocation;
import com.dyzx.hbb.modules.wms.entity.WmsLocation;
import com.dyzx.hbb.modules.wms.query.WmsLocationQuery;
import com.dyzx.hbb.modules.wms.service.WmsHouseService;
import com.dyzx.hbb.modules.wms.service.WmsLocationService;
import com.dyzx.hbb.modules.wms.vo.WmsLocationVO;
import com.dyzx.hbb.modules.wms.vo.WmsLocationUVO;
import com.dyzx.hbb.security.context.SecurityContextHolder;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 货位管理控制器
 * <p>处理仓库管理系统中货位的创建、查询和管理业务逻辑
 * <p>主要职责：
 * <ul>
 * <li>提供货位的基础CRUD操作</li>
 * <li>支持按名称、仓库、状态、编号等条件筛选货位信息</li>
 * <li>验证货位所属仓库的有效性</li>
 * <li>提供按仓库筛选的货位简易列表接口</li>
 * <li>基于仓库编号自动生成货位编号</li>
 * </ul>
 * <p>设计意图：为仓库管理模块提供精确的货位数据管理接口，
 * 确保货位信息的准确性和关联性，支持仓库空间的有效利用
 * 
 * @author 开发团队
 * @version 1.0.0
 * @since 2023-05-30
 */
@Tag(name = "货位管理", description = "货位相关接口")
@RestController
@RequestMapping("/wms/location")
public class WmsLocationController extends BaseValidController<
        WmsLocationService,
        WmsLocation,
        WmsLocationQuery,
        WmsLocationVO,
        WmsLocationUVO
        > {

    @Autowired
    private WmsLocationService service;

    @Autowired
    private WmsHouseService  houseService;

    @Override
    protected WmsLocationService getBaseService() {
        return service;
    }

    @Override
    protected MyLambdaQueryWrapper<WmsLocation> queryWrapper(WmsLocationQuery query) {
        MyLambdaQueryWrapper<WmsLocation> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.select(WmsLocation::getId)
                .select(WmsLocation::getNo)
                .select(WmsLocation::getName)
                .select(WmsLocation::getHouseId)
                .select(WmsLocation::getStatus);
        queryWrapper.orderByDesc(WmsLocation::getCreateDate);

        if (query != null) {
            queryWrapper.likeRight(query.getName() != null, WmsLocation::getName, query.getName())
                    .eq(query.getHouse_id() != null, WmsLocation::getHouseId, query.getHouse_id())
                    .eq(query.getStatus() != null, WmsLocation::getStatus, query.getStatus())

                    .likeRight( StringUtils.isNotBlank(query.getNo()), WmsLocation::getNo, query.getNo());
        }
        return queryWrapper;
    }

    @Override
    protected MyLambdaQueryWrapper<WmsLocation> queryInfoWrapper() {
        MyLambdaQueryWrapper<WmsLocation> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.orderByDesc(WmsLocation::getCreateDate);
        return queryWrapper;
    }

    @Override
    protected WmsLocation addValid(WmsLocationVO entity) {
        WmsHouse byId = houseService.getById(entity.getHouseId());
        if (byId == null) {
           throw new RuntimeException("仓库不存在");
        }
        Long organId = SecurityContextHolder.getOrganId();
        MyLambdaQueryWrapper<WmsLocation> queryWrapper = new MyLambdaQueryWrapper<>();
        queryWrapper.eq(WmsLocation::getOrganId, organId).eq(WmsLocation::getHouseId, entity.getHouseId());
        long count = service.count(queryWrapper);
        String no = Utils.generateYMOrderNumber(byId.getNo(), count);

        WmsLocation wmsHouse = new WmsLocation();
        BeanUtil.copyProperties(entity, wmsHouse);
        wmsHouse.setNo(no);

        return wmsHouse;
    }

    @Override
    protected WmsLocation editValid(WmsLocationUVO entity) {
        return null;
    }

    @Operation(summary = "简易列表")
    @GetMapping("/mini_list")
    public Result<List<Map<String, Object>>> miniListData(
            @RequestParam(value = "house_id") long house_id
    ) {
        if (house_id <= 0) {
            throw new RuntimeException("仓库不存在");
        }
        MyLambdaQueryWrapper<WmsLocation> wrapper = new MyLambdaQueryWrapper<>();
        wrapper.select(WmsLocation::getId, WmsLocation::getName);
        wrapper.select(WmsLocation::getName).as("label");
        wrapper.select(WmsLocation::getId).as("value");

        wrapper.orderByDesc(WmsLocation::getId);
        wrapper.eq(WmsLocation::getHouseId, house_id);


        List<Map<String, Object>> list = getBaseService().listMaps(wrapper);
        return list.isEmpty() ? Result.error(404, "暂无数据") : Result.ok(list);
    }
}